﻿namespace TradePublish.Common
{
    using System;
    using System.Collections;

    public class HTMLTable
    {
        public ArrayList rows = new ArrayList();
        private Stack zhanindex = new Stack();
        private Stack zhantag = new Stack();

        public HTMLTable(string htmlcocde)
        {
            htmlcocde = htmlcocde.Replace("<TR", "<tr");
            htmlcocde = htmlcocde.Replace("<Tr", "<tr");
            htmlcocde = htmlcocde.Replace("<tR", "<tr");
            htmlcocde = htmlcocde.Replace("<tr", "<tr");
            htmlcocde = htmlcocde.Replace("</TR", "</tr");
            htmlcocde = htmlcocde.Replace("</Tr", "</tr");
            htmlcocde = htmlcocde.Replace("</tR", "</tr");
            htmlcocde = htmlcocde.Replace("</tr", "</tr");
            htmlcocde = htmlcocde.Replace("<TD", "<td");
            htmlcocde = htmlcocde.Replace("<Td", "<td");
            htmlcocde = htmlcocde.Replace("<tD", "<td");
            htmlcocde = htmlcocde.Replace("<td", "<td");
            htmlcocde = htmlcocde.Replace("</TD", "</td");
            htmlcocde = htmlcocde.Replace("</Td", "</td");
            htmlcocde = htmlcocde.Replace("</tD", "</td");
            htmlcocde = htmlcocde.Replace("</td", "</td");
            htmlcocde = htmlcocde.Replace("<TABLE", "<table");
            htmlcocde = htmlcocde.Replace("</TABLE", "</table");
            htmlcocde = htmlcocde.Substring(htmlcocde.IndexOf("<table"));
            htmlcocde = htmlcocde.Substring(htmlcocde.IndexOf(">") + 1);
            htmlcocde = htmlcocde.Substring(0, htmlcocde.LastIndexOf("</table"));
            ArrayList list = new ArrayList();
            int num = 1;
            string str = "";
            for (int i = 0; i < (htmlcocde.Length - 1); i++)
            {
                if (htmlcocde.Substring(i, 1).Equals("<"))
                {
                    string str2 = htmlcocde.Substring(i, 3);
                    string str3 = htmlcocde.Substring(i, 4);
                    if ((str2.Equals("<tr") || str2.Equals("<td")) || str2.Equals("<ta"))
                    {
                        if (str2.Equals("<ta"))
                        {
                            num++;
                        }
                        if (str2.Equals("<tr") && (num == 1))
                        {
                            this.rows.Add(new ArrayList());
                        }
                        str = str + htmlcocde.Substring(i, 1);
                    }
                    else if ((str3.Equals("</tr") || str3.Equals("</td")) || str3.Equals("</ta"))
                    {
                        if (str3.Equals("</tr"))
                        {
                            if (num == 1)
                            {
                                str = str.Substring(str.IndexOf("<tr") + 1);
                                str = str.Substring(str.IndexOf(">") + 1).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                                int num3 = 0;
                                string str4 = "";
                                for (int j = 0; j < (str.Length - 1); j++)
                                {
                                    if (str.Substring(j, 1).Equals("<"))
                                    {
                                        string str5 = str.Substring(j, 3);
                                        string str6 = str.Substring(j, 4);
                                        if ((str5.Equals("<tr") || str5.Equals("<td")) || str5.Equals("<ta"))
                                        {
                                            if (str5.Equals("<ta"))
                                            {
                                                num3++;
                                            }
                                            str4 = str4 + str.Substring(j, 1);
                                        }
                                        else if ((str6.Equals("</tr") || str6.Equals("</td")) || str6.Equals("</ta"))
                                        {
                                            if (str6.Equals("</td"))
                                            {
                                                if (num3 == 0)
                                                {
                                                    str4 = str4.Substring(str4.IndexOf("<td") + 1);
                                                    str4 = str4.Substring(str4.IndexOf(">") + 1);
                                                    ((ArrayList) this.rows[this.rows.Count - 1]).Add(str4);
                                                    str4 = "";
                                                }
                                                else
                                                {
                                                    str4 = str4 + str.Substring(j, 1);
                                                }
                                            }
                                            else
                                            {
                                                if (str6.Equals("</ta"))
                                                {
                                                    num3--;
                                                }
                                                str4 = str4 + str.Substring(j, 1);
                                            }
                                        }
                                        else
                                        {
                                            str4 = str4 + str.Substring(j, 1);
                                        }
                                    }
                                    else
                                    {
                                        str4 = str4 + str.Substring(j, 1);
                                    }
                                }
                            }
                            else
                            {
                                str = str + htmlcocde.Substring(i, 1);
                            }
                        }
                        else
                        {
                            if (str3.Equals("</ta"))
                            {
                                num--;
                            }
                            str = str + htmlcocde.Substring(i, 1);
                        }
                    }
                    else
                    {
                        str = str + htmlcocde.Substring(i, 1);
                    }
                }
                else
                {
                    str = str + htmlcocde.Substring(i, 1);
                }
            }
        }
    }
}

